package com.nacos.consumer.config;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.nacos.consumer.handler.GlobalExceptionHandler;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RibbonConfig {

    @Bean
    @LoadBalanced
    @SentinelRestTemplate(blockHandler = "handleException",blockHandlerClass = GlobalExceptionHandler.class,
            fallback = "fallback",fallbackClass = GlobalExceptionHandler.class
    )
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    //自定义 负载均衡策略
    @Bean
    public IRule randomRule(){
        //return new RandomRule();
        //return new RoundRobinRule();
        return new RetryRule();
    }
}
